package com.web.Filter;

import com.web.entity.TestBiao;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *创建一个过滤器类，拦截对test目录下资源访问的请求，如果已经同意（教师申请状态改为1），放行对资源的访问否则将用户重定向到申请页面
 * 
 * @author 12270
 */

@WebFilter("/test/*")
public class TeacherApplyTestFilter implements Filter {
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletRequest req = (HttpServletRequest)request;
        HttpServletResponse resp = (HttpServletResponse)response;
        HttpSession session=req.getSession();
        TestBiao status=(TestBiao)session.getAttribute("teststatus");
        int a = status.getIstestfinish();
        
        if(a!=0){
            System.out.println(req.getSession().getAttribute("teststatus"));
            chain.doFilter(req, resp);
        }else{
            String contextPath = req.getContextPath()+"/";
            resp.sendRedirect(contextPath+"manageraccept.jsp");
        }
    }

    @Override
    public void destroy() {
    }
}
